(function($) {       
  $.fn.lin_star = function(options) {      
    var opts = $.extend({}, $.fn.lin_star.defaults, options);     
    return this.each(function() {  
		var $this=$(this),
		$stars=null,
		currentIndex=opts.startGrades || 0,
		starHtml='<ul class="'+opts.starClass+'" data_enabled="'+opts.enabled+'">';
		for(var i = 1; i <= opts.grades; i++)
		{
			starHtml+='<'+opts.starChild+'></'+opts.starChild+'>';
		}
		starHtml+='</ul>';
		$this.html(starHtml);
		$starParent= $this.find('.'+opts.starClass);
		$stars=$starParent.find(opts.starChild);
		currentStar();
		$stars.mouseenter(function(){
			var $_self=$(this);
			var $_parent=$_self.parents('.'+opts.starClass);
			if($_parent.attr('data_enabled')==0)return false;
			var index=$stars.index($_self);
			clearStar();
			for(var j = 0; j < index+1; j++)
			{
               $stars.eq(j).addClass(opts.currentStarClass);
            }
			if (typeof(opts.mouseenterFunction)=="function")
			{
				opts.mouseenterFunction(index+1);
			}			
		});

		$stars.mouseleave(function(){
			var $_self=$(this);
			var $_parent=$_self.parents('.'+opts.starClass);
			if($_parent.attr('data_enabled')==0)return false;			
			var index=$stars.index($_self);
			for(var j = 0; j < index+1; j++)
			{
               	$stars.eq(j).removeClass(opts.currentStarClass);
            }
			if (typeof(opts.mouseleaveFunction)=="function")
			{
				opts.mouseleaveFunction(index+1);
			}			
			currentStar();
		});

		$stars.click(function(){
			var $_self=$(this);
			var $_parent=$_self.parents('.'+opts.starClass);
			if($_parent.attr('data_enabled')==0)return false;			
			var index=$stars.index($_self);
			$_parent.attr('data_enabled',0);
			currentIndex=index+1;
			currentStar();
			if (typeof(opts.clickFunction)=="function")
			{
				opts.clickFunction(currentIndex);
			}
		});							
		
		function clearStar()
		{
			$stars.removeClass(opts.currentStarClass);
		}
		
		function currentStar()
		{
			for(var j = 0; j < currentIndex; j++)
			{
               $stars.eq(j).addClass(opts.currentStarClass);
			}
		}
		
    }); 

  };
         
  $.fn.lin_star.defaults ={
	  grades:5,//总分数
	  startGrades:0,//初始分数
	  enabled:1,//是否可打分
	  clickFunction:'',//单击回调事件
	  mouseenterFunction:'',//经过星星回调函数 
	  mouseleaveFunction:'',//离开星星回调函数
	  starClass:'lin_star',//星星列表样式
	  currentStarClass:'current',//星星选中样式
	  starChild:'li'//星星HTML元素
  };     
   
})(jQuery); 